<%@ include file="css/header.jsp" %>
<title>Edit user</title>
<%
String userId;
String manageUsers;
if((String)session.getAttribute("userId")==null||((String)session.getAttribute("userId")).trim().length() == 0) userId = "null";
else userId = (String)session.getAttribute("userId");
if((String)session.getAttribute("manageUsers")==null||((String)session.getAttribute("manageUsers")).trim().length() == 0) manageUsers = "null";
else manageUsers = (String)session.getAttribute("manageUsers");
	if(!(userId.equals("null"))){
		if(manageUsers.equals("1")){
%>
<script type="text/javascript">
$(document).ready(function() {
	var pageObj = $("#EditAdmin");
	var formObj = pageObj.find("#formEditAdmin");
	 formObj.validationEngine('attach');
	$('#mnu li').hover(
	        function () {
	            //show its submenu
	            $('ul', this).slideDown(100);
	 
	        }, 
	        function () {
	            //hide its submenu
	            $('ul', this).slideUp(100);         
	        }
	    );
	$.ajax({
        type: 'POST',
        url: '<%=request.getContextPath()%>/EditAdminServlet/DisplayData?id='+<%=request.getAttribute("id")%>,
        dataType: 'json',
      
        success: function(data){
           
            	bindJsonToHtml(data, pageObj);
            
        },
        error: function(){
            $.msgbox('error', {
                type : 'error'
            }, function(buttonPressed) {
                
            });
        }
    });

	

    
	$("#btnSave").click(function(){
		var formObj = $("#formEditAdmin");
		var oldpassword = formObj.find("#oldpassword").val();
		var password = formObj.find("#password").val();
		var conPassword = formObj.find("#confirmpassword").val();
		var data = formObj.serialize();

		if(conPassword == password){
			if(formObj.validationEngine('validate')) {
				if(oldpassword==("") && password==("") && conPassword==("") ){
					doSave();
				}else if(password != ("")){
					if(oldpassword != ("")){
						$.ajax({
				            type: 'POST',
				            url: '<%=request.getContextPath()%>/EditAdminServlet/CheckOldPassword',
				            dataType: 'json',
				            data: data,
				            success: function(data){
				                $.each(data, function(key, value) {
				                    //Validate is error send?
				                    if (key == 'error') {
				                        $.msgbox(value , {
				                            type : 'error'
				                        }, function(buttonPressed) {
				                           
				                        });
				                    } else { //No Error
				                    	doSave();
				                    }
				                    return false; //break each loop
				                });
				            },
				            error: function(jqXHR, textStatus, errorThrown){
	
				                $.msgbox('Error', {
				                    type : 'error'
				                }, function(buttonPressed) {
				                    // your code here
				                });
				            }
				        });
					}else if(oldpassword==("")){
						 $.msgbox('Please input old password.' , {
		                     type : 'error'
		                 }, function(buttonPressed) {
		                    
		                 });
					}
				}else{
						doSave();
				}
			}	
		}else{
			
		}	
	        return false;
	});
	
});   

function doSave(){
	var formObj = $("#formEditAdmin");
	var data = formObj.serialize();
	$.ajax({
        type: 'POST',
        url: '<%=request.getContextPath()%>/EditAdminServlet/SaveUser',
        dataType: 'json',
        data: data,
        success: function(data){
            $.each(data, function(key, value) {
                //Validate is error send?
                if (key == 'error') {
                    $.msgbox(value , {
                        type : 'error'
                    }, function(buttonPressed) {
                       
                    });
                } else { //No Error
                	 $.msgbox('Save success' , {
                         type : 'info'
                     }, function(buttonPressed) {
                    	 window.location.href = "ManageAdminServlet";
                     });
                }
                return false; //break each loop
            });
        },
        error: function(jqXHR, textStatus, errorThrown){

            $.msgbox('Error', {
                type : 'error'
            }, function(buttonPressed) {
            	
            });
        }
    });
}

function checkConfirm(){
	var formObj = $("#formEditAdmin");
	var password = formObj.find("#password").val();
	var conPassword = formObj.find("#confirmpassword").val();
	if(conPassword != password){
		$("#checkconfirm").text("Password does not match");
	}else $("#checkconfirm").text("");
}
</script>
<body>
<div id="jp-main">
        <div class="jp-sheet">
            <div class="jp-sheet-tl"></div>
            <div class="jp-sheet-tr"></div>
            <div class="jp-sheet-bl"></div>
            <div class="jp-sheet-br"></div>
            <div class="jp-sheet-tc"></div>
            <div class="jp-sheet-bc"></div>
            <div class="jp-sheet-cl"></div>
            <div class="jp-sheet-cr"></div>
            <div class="jp-sheet-cc"></div>
            <div class="jp-sheet-body">
                <div class="jp-header">
                    <div class="jp-header-center">
                        <div class="jp-header-png"></div>
                        <div class="jp-header-jpeg"></div>
                    </div>
                        <div class="jp-header-fluid-object"></div>
                </div>
                <div class="jp-nav">
                	<div class="l"></div>
                	<div class="r"></div>
                	<ul class="jp-menu">
                		
                			<%if (session.getAttribute("tcdTracking").equals("1")){%>
					    	  <li>
					    	  	<a href="ViewServlet"><span class="l"></span><span class="r"></span><span class="t">TCD Tracking</span></a>                		
                			  </li>
					      	<%} %>
					      	<%if (session.getAttribute("rdTracking").equals("1")){%>
					    	  <li>
					    	  	<a href="ViewServletRD"><span class="l"></span><span class="r"></span><span class="t"></span><span class="t">RD Tracking</span></a>                		
                			  </li>
					      	<%} %>
					      	<%if (session.getAttribute("tcdBilling").equals("1")){%>
					    	  <li>
					    	  	<a href="TCDBillingServlet"><span class="l"></span><span class="r"></span><span class="t"></span><span class="t">TCD Billing</span></a>                		
                			  </li>
					      	<%} %>
					      	<%if (session.getAttribute("rdBilling").equals("1")){%>
					    	  <li>
					    	  	<a href="RDBilling"><span class="l"></span><span class="r"></span><span class="t"></span><span class="t">RD Billing</span></a>                		
                			  </li>
					      	<%} %>
					      	<%if (!(userId.equals("0"))){%>
					    	  <li>
					    	  	<a href="EditProfile.jsp"><span class="l"></span><span class="r"></span><span class="t"></span><span class="t">Edit profile</span></a>                		
                			  </li>
					      	<%} %>
                			
           		  		<li>
                			<a href="#" class="active"><span class="l"></span><span class="r"></span><span class="t">User Management</span></a>
							<ul>
								<%if (session.getAttribute("manageUsers").equals("1")){%>
						    		<li><a href="ManageAdminServlet">Manage users</a></li>
						      	<%} %>
						      	<%if (session.getAttribute("manageGroups").equals("1")){%>
					    			<li><a href="ManageGroupServlet">Manage groups</a></li>
					      		<%} %>
					      		<%if (session.getAttribute("userLog").equals("1")){%>
					    			<li><a href="UserLogServlet">User log</a></li>
					      		<%} %>
           					</ul>
               		  	</li>		
           		  	  	<li>
                			<a href="Logout"><span class="l"></span><span class="r"></span><span class="t">Logout</span></a>                		
                	  	</li>
               	  </ul>
                </div>
                <div class="jp-content-layout">
                  <div class="jp-post-body">
                     <h2 class="jp-postheader">Manage users</h2>
                      <div class="jp-postcontent">
                      <div id="EditAdmin">
                      <fieldset id="fsEditAdmin">
			            <legend class="pageTitleForm"><h3>Edit user</h3></legend>
			            <form id="formEditAdmin" method="POST" class="form" >
			                <fieldset>
			                    <ol>
			                    	 <li class="">
			                            <div class="colTile">
			                              <label for="username">Username</label>
			                            </div>
			                            <div class="colContent">
			                               <label id="username"></label>
			                               <input id="id"  name="id"type="hidden" value="<%=request.getAttribute("id")%>"></input>
			                            </div>
			                        </li>
			                        <li class="">
			                            <div class="colTile">
			                              <label for="oldpassword">Old password</label>
			                            </div>
			                            <div class="colContent">
			                                <input type="password" name="oldpassword" id=oldpassword value="" />
			                            </div>
			                        </li>
			                         <li class="">
			                            <div class="colTile">
			                              <label for="password">Password</label>
			                            </div>
			                            <div class="colContent">
			                                <input type="password" name="password" id="password" value="" OnChange="JavaScript:checkConfirm();"/>
			                            </div>
			                        </li>
			                                             <li class="">
			                            <div class="colTile">
			                              <label for="confirmpassword">Confirm Password</label>
			                            </div>
			                            <div class="colContent">
			                                <input type="password" name="confirmpassword" id="confirmpassword" value="" OnChange="JavaScript:checkConfirm();" />
			        						<span id = checkconfirm></span>
			                            </div>
			                        </li>
			                         <li class="">
			                            <div class="colTile">
			                              <label for="firstname">&nbsp;</label>
			                            </div>
			                            <div class="colContent">
			                               <input type="radio" id="rdoForm" name="rdoRecordType" value="Mr" checked><label>Mr.</label></input>
			                               <input type="radio" id="rdoForm" name="rdoRecordType" value="Mrs"><label>Mrs.</label></input>
			                               <input type="radio" id="rdoForm" name="rdoRecordType" value="Miss"><label>Miss</label></input>
			                               <input type="radio" id="rdoForm" name="rdoRecordType" value="Ms"><label>Ms.</label></input>
			                            </div>
			                        </li>
			                        <li class="column-2">
			                            <div class="colTile">
			                              <label for="firstname">First name</label>
			                            </div>
			                            <div class="colContent">
			                                <input type="text" name="firstname" id="firstname" value="" />
			                            </div>
			                        </li>
			                        <li class="column-2">
			                            <div class="colTile">
			                              <label for="lastname">Last name</label>
			                            </div>
			                            <div class="colContent">
			                                <input type="text" name="lastname" id="lastname" value=""/>
			                            </div>
			                        </li>
			                        <li class="">
			                            <div class="colTile">
			                              <label for="address">Address</label>
			                            </div>
			                            <div class="colContent">
			                                <input type="text" name="address" id="address" value=""  class="width-100" />
			                            </div>
			                        </li>
			                        <li class="">
			                            <div class="colTile">
			                              <label for="tel">Tel.</label>
			                            </div>
			                            <div class="colContent">
			                                <input type="text" name="tel" id="tel" value="" />
			                            </div>
			                        </li>
			                        <li class="">
			                            <div class="colTile">
			                              <label for="fax">Fax.</label>
			                            </div>
			                            <div class="colContent">
			                                <input type="text" name="fax" id="fax" value="" />
			                            </div>
			                        </li>
			                        <li class="">
			                            <div class="colTile">
			                              <label for="mail">E-mail</label>
			                            </div>
			                            <div class="colContent">
			                                <input type="text" name="email" id="email" class="validate[custom[email]]"  value=""  />
			                            </div>
			                        </li>
			                        <li>
			                            <div  class="colTile">
			                                &nbsp;
			                            </div>
			                            <div class="colContent">
			                                <div id="btnCommand">
			                                  <button id="btnClear" type="reset" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-icon-primary"><span class="ui-button-icon-primary ui-icon ui-icon-refresh"></span><span class="ui-button-text">Clear</span></button>
			                                   <button id="btnSave" type="button" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-icon-primary" ><span class="ui-button-icon-primary ui-icon ui-icon-disk"></span><span class="ui-button-text">Save</span></button>
			                                </div>
			                            </div>
			                        </li>
			                    </ol>
			                </fieldset>
			            </form>
                            <div id="content-placeholder">
                                <div id="jqgrid">
                                    <table id="grdTransaction"></table>
                                    <div id="pagerTransaction"></div>
                                </div>
                            </div>
			        	</fieldset>
                              </div> 
			        	
                      </div>
                  </div>
                </div>
            </div>
        </div>
</div>
    
</body>	
<%
		}else{
%>
<script type="text/javascript">
    	window.location.href = "EditProfile";
</script>
<%
		}
	}else{
%>
<script type="text/javascript">
	window.location.href = "LoginServlet";
</script>
<%
	}
	
%>
</html>